<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row :gutter="15">
        <el-col :lg="4" :md="8">
          <el-form-item label="搜索:">
            <el-input
              clearable
              v-model="where.keywords"
              placeholder="请输入姓名或学籍号"/>
          </el-form-item>
        </el-col>
     <el-col :lg="4" :md="8">
       <el-form-item label="统计学年:">
         <el-select
           filterable
           clearable
           v-model="where.annual"
           @change="$forceUpdate()"
           size="small"
           placeholder="-学年统计-"
           class="ele-block">
           <el-option v-for="item in xuenianList" :key="item.id" :label="item.name" :value="item.id"/>
         </el-select>
       </el-form-item>
     </el-col>

     <el-col :lg="8" :md="10">
       <el-form-item label="统计日期:">
         <el-col :lg="8" :md="10">
           <el-date-picker
             type="date"
             class="ele-fluid"
             v-model="where.startTime"
             value-format="yyyy-MM-dd"
             format="yyyy-MM-dd"
             placeholder="请选择开始日期"/>
         </el-col>
         <el-col :lg="2" :md="12">
           至
         </el-col>
         <el-col :lg="8" :md="12">
           <el-date-picker
             type="date"
             class="ele-fluid"
             v-model="where.endTime"
             value-format="yyyy-MM-dd"
             format="yyyy-MM-dd"
             placeholder="请选择结束日期"/>
         </el-col>
       </el-form-item>
     </el-col>
     <el-col :lg="4" :md="12">
       <el-form-item label="模式:">
         <el-select
           clearable
           v-model="where.hasTest"
           placeholder="请选择模式"
           class="ele-fluid">
           <el-option label="测试模式" value="1"/>
           <el-option label="训练模式" value="0"/>
         </el-select>
       </el-form-item>
     </el-col>
     <el-col :lg="4" :md="8">
       <el-form-item label="项目:">
          <project-select  v-model="where.projectId" @chageProject="chageProject" @chageProjectId="chageProjectId"></project-select>
       </el-form-item>
     </el-col>

     <el-col :lg="4" :md="8">
       <el-form-item ref="grade" prop="grade" label="年级:">
         <grade-select  v-model="where.gradeId" @changeGrade="changeGrade" @changeGradeId="changeGradeId"></grade-select>
       </el-form-item>
     </el-col>


     <el-col :lg="4" :md="12">
       <el-form-item label="院系类型:">
         <el-select
           clearable
           v-model="where.stype"
           placeholder="请选择院系类型"
           @change="getDepartmentsList"
           class="ele-fluid">
                         <el-option label="自然院系" value="1"/>
                         <el-option label="体育院系" value="2"/>
                       </el-select>
       </el-form-item>
     </el-col>
     <el-col :lg="4" :md="8">
       <el-form-item label="院系:">
         <el-select
           filterable
           clearable
           v-model="where.departmentsId"
           @change="$forceUpdate();getProfessionalList();getClassList()"
           size="small"
           placeholder="-请选择院系-"
           class="ele-block">
           <el-option v-for="item in departmentsList" :key="item.id" :label="item.name" :value="item.id"/>
         </el-select>
       </el-form-item>
     </el-col>
     <el-col :lg="4" :md="8">
       <el-form-item label="专业:" prop="professional">
           <el-select
             clearable
             v-model="where.professionalId"
             @change="$forceUpdate();getClassList()"
             size="small"
             placeholder="-请选择专业-"
             class="ele-block">
             <el-option v-for="item in professionalList" :key="item.id" :label="item.name" :value="item.id"/>
           </el-select>
       </el-form-item>
     </el-col>
     <el-col :lg="4" :md="8">
       <el-form-item label="班级:" prop="professional">
           <el-select
             clearable
             v-model="where.classId"
             @change="$forceUpdate()"
             size="small"
             placeholder="-请选择班级-"
             class="ele-block">
             <el-option v-for="item in classList" :key="item.id" :label="item.name" :value="item.id"/>
           </el-select>
       </el-form-item>
     </el-col>
     <el-col :lg="4" :md="8">
       <el-form-item label="性别:">
         <el-select
           clearable
           v-model="where.gender"
           placeholder="请选择性别"
           class="ele-fluid">
                         <el-option label="男" value="1"/>
                         <el-option label="女" value="2"/>
                       </el-select>
       </el-form-item>
     </el-col>

        <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                @click="reload">查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        v-if="showtable"
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
                    <el-button
                      @click="exportData"
                      type="primary"
                      icon="el-icon-download"
                      class="ele-btn-icon"
                      size="small">导出
                    </el-button>
                  </template>
 <!-- 性别列 -->
        <template slot="gender" slot-scope="{row}">
          <el-tag
            :type="['success', 'primary', 'warning'][row.gender-1]"
            size="mini">
            {{ ['男', '女', '保密'][row.gender - 1] }}
          </el-tag>
        </template>
                        </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <members-edit
      :data="current"
      :visible.sync="showEdit"
      @done="reload"/>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import MembersEdit from './memb-edit';
import gradeSelect from '@/components/gradeSelect'
import projectSelect from '@/components/projectSelect'
import XLSX from 'xlsx';

export default {
  name: 'ExamMembers',
  components: {MembersEdit,gradeSelect,projectSelect},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      showtable:false,
      // 表格数据接口
      url: '/memb/index',
      // 表格列配置
      columns: [

        {
          prop: 'id',
          label: 'ID',
          width: 60,
          align: 'center',
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: 'pro_name',
          label: '项目',
          minWidth: 200,
          align: 'center',
        },
        {
          prop: 'realname',
          label: '姓名',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
                          {
          prop: 'roll',
          label: '学籍号',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
 {
          prop: 'grade_name',
          label: '年级',
          showOverflowTooltip: true,
          width: 100,
          align: 'center',
        },
        {
          prop: 'prof_name',
          label: '专业',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },
      {
          prop: 'class_name',
          label: '专业班级',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
        },

        {
          prop: 'gender',
          label: '性别',
          showOverflowTooltip: true,
          minWidth: 200,
          align: 'center',
          slot: 'gender'
        },
        {
           prop: 'avg',
           label: '平均分',
           showOverflowTooltip: true,
           minWidth: 200,
           align: 'center',
         },
         {
           prop: 'youRate',
           label: '优良率',
           showOverflowTooltip: true,
           minWidth: 200,
           align: 'center',
         },
         {
           prop: 'hegeRate',
           label: '合格率',
           showOverflowTooltip: true,
           minWidth: 200,
           align: 'center',
         }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      projectList:[],
      xuenianList:[],
      classList: [],
      gradeList:[],
      departmentsList:[],
      professionalList:[],
    };
  },
  mounted() {

   // 获取项目
   //this.getProjectList();
   // 获取学年
   this.getXueList();
   this.getProfessionalList();
   this.getDepartmentsList();
  },
  methods: {
    changeGrade(val){
      this.where.gradeId = val;
      if(this.where.projectId&&this.where.gradeId){
        this.showtable = true
      }
      this.getClassList();
    },
    chageProject(val){
      this.where.projectId = val;
      if(this.where.projectId&&this.where.gradeId){
        this.showtable = true
      }
    },
    chageProjectId(val){
      this.where.projectId = val;
      this.$forceUpdate();
    },
    changeGradeId(val){
      this.where.gradeId = val;
      this.$forceUpdate();
      this.getClassList();
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {
        projectId:this.where.projectId,
        gradeId:this.where.gradeId
      };
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 导出数据 */
    exportData() {
      let array = [['ID', '项目', '姓名', '学籍号', '年级','专业','专业班级','性别', '平均分','优良率','合格率']];
      const loading = this.$loading({lock: true});
      this.$http.get('/memb/index?page=1&limit=999999&projectId='+this.where.projectId+'&gradeId='+this.where.gradeId+'&annual='+this.where.annual+'&startTime='+this.where.startTime+'&endTime='+this.where.endTime+'&hasTest='+this.where.hasTest+'&stype='+this.where.stype+'&departmentsId='+this.where.departmentsId+'&professionalId='+this.where.professionalId+'&classId='+this.where.classId+'&gender='+this.where.gender+'&keywords='+this.where.keywords).then(res => {
        loading.close();
        if (res.data.code === 0) {
          res.data.data.forEach(d => {
            array.push([
              d.id,
              d.pro_name,
              d.realname,
              d.roll,
              d.grade_name,
              d.prof_name,
              d.class_name,
              d.gender_str,
              d.avg,
              d.youRate,
              d.hegeRate,
             // this.$util.toDateString(d.createTime)
            ]);
          });
          this.$util.exportSheet(XLSX, array, '学生体测');
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /**
      * 获取专业列表
      */
     getProfessionalList() {
      if(this.where.professionalId){
        delete this.where.classId
      }
       this.$http.post('/professional/getProfessionalList',{type: this.where.stype,sid:this.where.departmentsId}).then(res => {
         if (res.data.code === 0) {
           this.professionalList = res.data.data;
         } else {
           this.$message.error(res.data.msg);
         }
       }).catch(e => {
         this.$message.error(e.message);
       });
       this.getClassList();
     },
     /**
      * 获取院系列表
      */
      getDepartmentsList() {
       if(this.where.stype){
         delete this.where.departmentsId
         delete this.where.professionalId
         delete this.where.classId
         // this.where.departmentsId = '';
         // this.where.professionalId = '';
         // this.where.classId = '';
         // this.$forceUpdate();
       }
       this.$http.post('/departments/getDepartmentsList',{type: this.where.stype}).then(res => {
          if (res.data.code === 0) {
            this.departmentsList = res.data.data;
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          this.$message.error(e.message);
        });
       this.getProfessionalList();
       this.getClassList();
    },
    /**
        * 获取组织列表
        */
       getProjectList() {
         this.$http.get('/project/getProjectList').then(res => {
           if (res.data.code === 0) {
             this.projectList = res.data.data;
             this.where.projectId=this.projectList && this.projectList.length > 0 ? this.projectList[0].id : ''
             return this.where.projectId
           } else {
             this.$message.error(res.data.msg);
           }
         }).catch(e => {
           this.$message.error(e.message);
         });
       },
       getXueList() {
         this.$http.get('/annual/getXueList').then(res => {
           if (res.data.code === 0) {
             this.xuenianList = res.data.data;
           } else {
             this.$message.error(res.data.msg);
           }
         }).catch(e => {
           this.$message.error(e.message);
         });
       },
    /**
        * 获取班级列表
        */
       getClassList() {
         if(this.where.departmentsId){
           this.where.class = null;
           this.$forceUpdate();
         }
         this.$http.post('/clss/getClassList',{yid:this.where.departmentsId,gid:this.where.gradeId,pid:this.where.professionalId,stype:this.where.stype}).then(res => {
           if (res.data.code === 0) {
             this.classList = res.data.data;
           } else {
             this.$message.error(res.data.msg);
           }
         }).catch(e => {
           this.$message.error(e.message);
         });
       },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/members/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的学生体测吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/members/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
                                                                                                                                                                                                                            }
}
</script>

<style scoped>
</style>
